#!/usr/bin/env python3
# -*- coding:utf-8 -*-

## pick up files from a folder according to file list
## Usage: pickup_files FROM_FOLDER TO_FOLDER FILELIST

import os
import codecs
import sys
import shutil

FROM_FOLDER = 'from'
TO_FOLDER = 'to'
FILELIST = 'filelist.txt'


def copyfile(src, dst):
    if not os.path.isfile(src):
        print("%s not exist!" % (src))
    else:
        fpath,fname=os.path.split(dst)
        if not os.path.exists(fpath):
            os.makedirs(fpath)
        shutil.copy(src, dst)
        # print("copy %s -> %s" % (src, dst))

if __name__ == '__main__':
    if len(sys.argv) == 4:
        FROM_FOLDER = sys.argv[1]
        TO_FOLDER = sys.argv[2]
        FILELIST = sys.argv[3]
    else:
        print('Usage: pickup_files FROM_FOLDER TO_FOLDER FILELIST')

    # Make to folder if not exist
    if not os.path.exists(TO_FOLDER):
        os.makedirs(TO_FOLDER)

    # Copy each file from list
    with open(FILELIST,'r') as f:
        for line in f:
            fn = line.strip()
            copyfile(FROM_FOLDER+fn, TO_FOLDER+fn)
